library(plyr)
library(igraph)
library(fmsb)
library(Hmisc)
library(MASS)
library(stargazer)
library(tidyverse)
library(multcomp)
library(gsynth)
data(gsynth)
library(panelView)
library(modelsummary) # https://modelsummary.com/articles/modelsummary.html#new-models-and-custom-statistics

setwd("~/Dropbox/China Huawei Paper/Production Materials/Replication/")
master.data <- read.csv("dataFile_China_Huawei.csv"); head(master.data)


# Chinese Aid
dat <- master.data
dat$treated = 0
threshold=mean(dat$china.oda, na.rm=T)
dat = as.data.frame(dat %>%
  group_by(cabb) %>%
  dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
         treated = ifelse(china.oda > threshold, 1, treated)) %>%
  fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$v2x_polyarchy>0.42,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
nrow(dat);nrow(sub)

out1 <- gsynth(v2smgovfilprc ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out1, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Democracies\nSocial Media Filtering")
out1$est.avg
out1Aid = out1$est.avg

out2 <- gsynth(v2smgovshut ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out2, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Democracies\nInternet Shutdowns")
out2$est.avg
out2Aid = out2$est.avg

out3 <- gsynth(v2smgovsmmon ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out3, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Democracies\nSocial Media Monitoring")
out3$est.avg
out3Aid = out3$est.avg

out4 <- gsynth(v2smarrest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out4, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Democracies\nArrests for Online Content")
out4$est.avg
out4Aid = out4$est.avg


# Chinese Military
dat <- master.data
dat$treated = 0
threshold=mean(dat$china.military, na.rm=T)
dat = as.data.frame(dat %>%
  group_by(cabb) %>%
  dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
         treated = ifelse(china.military > threshold, 1, treated)) %>%
  fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$v2x_polyarchy>0.42,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
nrow(dat);nrow(sub)

out1 <- gsynth(v2smgovfilprc ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out1, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Democracies\nSocial Media Filtering")
out1$est.avg
out1Mil = out1$est.avg

out2 <- gsynth(v2smgovshut ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out2, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Democracies\nInternet Shutdowns")
out2$est.avg
out2Mil = out2$est.avg

out3 <- gsynth(v2smgovsmmon ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out3, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Democracies\nSocial Media Monitoring")
out3$est.avg
out3Mil = out3$est.avg

out4 <- gsynth(v2smarrest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out4, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Democracies\nArrests for Online Content")
out4$est.avg
out4Mil = out4$est.avg

# Chinese OOF
dat <- master.data
dat$treated = 0
threshold=mean(dat$china.oof, na.rm=T)
dat = as.data.frame(dat %>%
  group_by(cabb) %>%
  dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
         treated = ifelse(china.oof > threshold, 1, treated)) %>%
  fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$v2x_polyarchy>0.42,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
nrow(dat);nrow(sub)

out1 <- gsynth(v2smgovfilprc ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out1, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Democracies\nSocial Media Filtering")
out1$est.avg
out1Fin = out1$est.avg

out2 <- gsynth(v2smgovshut ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out2, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Democracies\nInternet Shutdowns")
out2$est.avg
out2Fin = out2$est.avg

out3 <- gsynth(v2smgovsmmon ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out3, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Democracies\nSocial Media Monitoring")
out3$est.avg
out3Fin = out3$est.avg

out4 <- gsynth(v2smarrest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out4, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Democracies\nArrests for Online Content")
out4$est.avg
out4Fin = out4$est.avg

# export table
ti = data.frame(rbind(out1Aid,out1Mil,out1Fin))
ti$term = c("Development Aid (Value \u2265 Mean)","Weapons Transfers (Value \u2265 Mean)","Overseas Official Finance (Value \u2265 Mean)")
ti = ti[,c("term","Estimate","S.E.","p.value")]
names(ti) = c("term","estimate","std.error","p.value")
out1 = list(tidy=ti)
class(out1) = "modelsummary_list"

ti = data.frame(rbind(out2Aid,out2Mil,out2Fin))
ti$term = c("Development Aid (Value \u2265 Mean)","Weapons Transfers (Value \u2265 Mean)","Overseas Official Finance (Value \u2265 Mean)")
ti = ti[,c("term","Estimate","S.E.","p.value")]
names(ti) = c("term","estimate","std.error","p.value")
out2 = list(tidy=ti)
class(out2) = "modelsummary_list"

ti = data.frame(rbind(out3Aid,out3Mil,out3Fin))
ti$term = c("Development Aid (Value \u2265 Mean)","Weapons Transfers (Value \u2265 Mean)","Overseas Official Finance (Value \u2265 Mean)")
ti = ti[,c("term","Estimate","S.E.","p.value")]
names(ti) = c("term","estimate","std.error","p.value")
out3 = list(tidy=ti)
class(out3) = "modelsummary_list"

ti = data.frame(rbind(out4Aid,out4Mil,out4Fin))
ti$term = c("Development Aid (Value \u2265 Mean)","Weapons Transfers (Value \u2265 Mean)","Overseas Official Finance (Value \u2265 Mean)")
ti = ti[,c("term","Estimate","S.E.","p.value")]
names(ti) = c("term","estimate","std.error","p.value")
out4 = list(tidy=ti)
class(out4) = "modelsummary_list"

myrows = data.frame(list(c("Country Fixed Effects",rep("Yes",4))
                         ,c("Year Fixed Effects",rep("Yes",4))
                         ,c("Control Variables",rep("Yes",4))
))

export = modelsummary(list("Internet Filtering" = out1
                           , "Internet Shutdowns" = out2
                           ,"Social Media Monitoring"=out3
                           ,"Arrests for Political Content"=out4
)
,add_rows=data.frame(t(myrows))
, stars=c('*' = .1, '**' = 0.05, '***' = .01)
, output="latex_tabular"
)
export = gsub("≥", "$\\\\ge$", export)
export


